import aside  from "./aside.js";
import goBack from "./goTop.js";
import loaddata from "./loaddata.js";
import compare from "./compare.js";
import carts from "./carts.js";
export default function(){
    var ls = localStorage.getItem("user");
    if(ls !== null){
        var la = JSON.parse(ls);
        var html = "";
        $.each(la,function(index,item){
            html = `ip${item.user_phone}`;
        })
        $(".login_id").html(html);
        $(".login_id").on("click",$.proxy(function(){
            if(window.confirm("是否退出登录")){
                $(".login_id").html("登录");
                $(".login_id").removeAttr("href");
            }
        },this));
        // $(".login_id").attr("href","http://localhost/VSCode/JuanPi_web_project/src/login.html");
    }
    aside().init();
    goBack().init();
    var detail_pic = $(".detail-pic ul");
    var pic        = $(".pic");
    var title      = $(".detail-wrap h1");
    var activity_item = $(".activity-item");
    var tm_price   = $(".tm-price");
    var food_wrap  = $(".food-wrap");
    var size_wrap  = $(".size");
    var plaxe_explain   = $(".place-explain");
    var xiangqing  = $(".xiangqing");
    loaddata()
    .done(function(res){
        // 渲染
        var goods_id = location.hash.substring(1);
        var json = compare().init( res , goods_id );
        $(detail_pic).html(json.html);
        $(pic).html(json.text);
        $(title).html(json.title);
        $(activity_item).html(json.promotion);
        $(tm_price).html(json.price);
        $(food_wrap).html(json.choose);
        $(size_wrap).html(json.size);
        $(plaxe_explain).html(json.addr);
        $(xiangqing).html(json.lazy);
        // 效果
        $(detail_pic).on("mouseenter","li",function(){
            $(this).addClass("cur")
            .siblings()
            .removeClass("cur");
            var pic_img = $(".pic img");
            var src = $(this).attr("big-src");
            $(pic_img).attr("src",src);
        })
        // 懒加载
        var img_load = $(".goods_desc_img");
        var timer = null;
        $(window).scroll(function(){
            clearTimeout(timer);
            timer = setTimeout(function(){
                var scroll = $("html,body").scrollTop();
                showAll(scroll,img_load);
            },100);
        })
        function showAll(data,img){
            $.each(img,function(index,item){
                var src = $(item).attr("d-src");
                if(data >= $(item).offset().top - 800){    
                    $(item).attr("src",src);
                }
            })
        }
        carts(goods_id).init();
    })

}